package cn.innosmart.aq.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.WorkLogTemp;

/**
 * Created by David on 2015/7/24.
 */
public class WorkLogTempListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private ArrayList<WorkLogTemp> workLogs;
    private View.OnClickListener onClickListener;
    //    private View.OnClickListener onAddTemperOnClick;
    private boolean isModify = false;

    /**
     * 日志模板适配器
     *
     * @param context
     * @param workLogs 日志模板对象列表
     */
    public WorkLogTempListAdapter(Context context, ArrayList<WorkLogTemp> workLogs) {
        mInflater = LayoutInflater.from(context);
        mContext = context;
        this.workLogs = workLogs;
    }

    @Override
    public int getCount() {
        return workLogs.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return workLogs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_work_log_temp, null);
            holder.tvLogTitle = (TextView) convertView.findViewById(R.id.tv_log_title);
            holder.ivLogIcon = (ImageView) convertView.findViewById(R.id.iv_log_icon);
//            holder.tvCheck = (TextView) convertView.findViewById(R.id.tv_check);
            holder.tvDelete = (TextView) convertView.findViewById(R.id.tv_delete);
            holder.rlAdd = (RelativeLayout) convertView.findViewById(R.id.rl_add);
            holder.rlData = (RelativeLayout) convertView.findViewById(R.id.rl_data);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == workLogs.size()) {
            holder.rlAdd.setVisibility(View.VISIBLE);
            holder.rlData.setVisibility(View.INVISIBLE);
            if (onClickListener != null)
                holder.rlAdd.setOnClickListener(onClickListener);
        } else {
            holder.rlAdd.setVisibility(View.INVISIBLE);
            holder.rlData.setVisibility(View.VISIBLE);
            WorkLogTemp workLog = workLogs.get(position);
            holder.tvLogTitle.setText(workLog.getTitle());
            holder.rlData.setTag(workLog);
            holder.tvDelete.setTag(workLog);
            if (onClickListener != null)
                holder.rlData.setOnClickListener(onClickListener);
            if (isModify) {
                holder.tvDelete.setVisibility(View.VISIBLE);
                if (onClickListener != null)
                    holder.tvDelete.setOnClickListener(onClickListener);
            } else {
                holder.tvDelete.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    public void refresh(ArrayList<WorkLogTemp> workLogs) {
        this.workLogs = workLogs;
        notifyDataSetChanged();
    }

    public void startModify(boolean change) {
        this.isModify = change;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    //增加日志模板
    public void add(WorkLogTemp workLog) {
        workLogs.add(workLog);
        notifyDataSetChanged();
    }

    /**
     * 删除日志模板
     * @param id
     */
    public void remove(String id) {
        if (TextUtils.isEmpty(id)) {
            return;
        }
        for (WorkLogTemp workLog : workLogs) {
            if (id.equals(workLog.getId())) {
                workLogs.remove(workLog);
                break;
            }
        }
        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView tvLogTitle;
        ImageView ivLogIcon;
        TextView tvCheck;
        TextView tvDelete;
        RelativeLayout rlAdd;
        RelativeLayout rlData;
    }

}
